package me.albert.entitylimiter.listeners;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import me.albert.entitylimiter.utils.inventory.PagedInv;
import me.albert.entitylimiter.utils.inventory.TiHolder;

import org.bukkit.Material;
import org.bukkit.block.Container;
import org.bukkit.block.ShulkerBox;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.ItemDespawnEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.metadata.MetadataValue;

public class ItemListener implements Listener {
    public static HashMap<UUID, Integer> pages;

    private static boolean isVanished(final Player player) {
        for (final MetadataValue meta : player.getMetadata("vanished")) {
            if (meta.asBoolean()) {
                return true;
            }
        }
        return false;
    }

    public static void sellItem(ItemStack itemStack, final String info, final Item item) {
        itemStack = itemStack.clone();
        if (itemStack == null || itemStack.getType().equals(Material.AIR)) {
            return;
        }
        ItemMeta meta = itemStack.getItemMeta();
        final List<String> lore = meta.hasLore() ? meta.getLore() : new ArrayList<>();
        lore.add("§●§7[§●点§●击§●领§●取§●]§●");
        meta.setLore(lore);
        itemStack.setItemMeta(meta);
        PagedInv.addItem(itemStack);
    }

    @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
    public void onBreak(final BlockBreakEvent e) {
        if (e.getBlock().getState() instanceof Container) {
            if (e.getBlock().getState() instanceof ShulkerBox) {
                return;
            }
            final Container c = (Container) e.getBlock().getState();
            final List<ItemStack> items = new ArrayList<>();
            for (final ItemStack is : c.getInventory()) {
                if (is != null) {
                    items.add(is);
                }
            }
            if (items.size() > 20) {
                e.setCancelled(true);
                e.getPlayer().sendMessage("§7[§b提示§7] §c此箱子内物品过多,无法摧毁,如果掉落物过于密集服务器会自动清理!");
            }
        }
    }

    @EventHandler
    public void onClick(final InventoryClickEvent e) {
        if (!(e.getInventory().getHolder() instanceof TiHolder)) {
            return;
        }
        e.setCancelled(true);
        if (e.getClickedInventory() == null || !(e.getClickedInventory().getHolder() instanceof TiHolder)) {
            return;
        }
        final ItemStack item = e.getCurrentItem();
        if (item == null) {
            return;
        }
        final TiHolder tiHolder = (TiHolder) e.getInventory().getHolder();
        ItemListener.pages.put(e.getWhoClicked().getUniqueId(), tiHolder.getPage());
        if (e.getSlot() == 45) {
            if (tiHolder.getPage() == 0) {
                return;
            }
            e.getWhoClicked().openInventory(PagedInv.invs.get(tiHolder.getPage() - 1));
        }
        if (e.getSlot() == 53) {
            if (tiHolder.getPage() + 1 == PagedInv.invs.size()) {
                return;
            }
            e.getWhoClicked().openInventory(PagedInv.invs.get(tiHolder.getPage() + 1));
        }
        if (!item.hasItemMeta() || !item.getItemMeta().hasLore()) {
            return;
        }
        if (e.getWhoClicked().getInventory().firstEmpty() == -1) {
            e.getWhoClicked().sendMessage("§c你的背包已经满了!");
            return;
        }
        boolean has = false;
        final List<String> nlore = new ArrayList<>();
        for (final String s : item.getItemMeta().getLore()) {
            if (s.equalsIgnoreCase("§●§7[§●点§●击§●领§●取§●]§●")) {
                has = true;
            } else {
                nlore.add(s);
            }
        }
        if (!has) {
            return;
        }
        final ItemStack add = item.clone();
        ItemMeta meta = add.getItemMeta();
        meta.setLore(nlore);
        add.setItemMeta(meta);
        item.setAmount(0);
        e.getWhoClicked().getInventory().addItem(add);
    }

    @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
    public void onDespawn(final ItemDespawnEvent e) {
        final Item item = e.getEntity();
        if (item.isDead()) {
            return;
        }
        item.remove();
        sellItem(item.getItemStack(), "despawned", item);
    }

    @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
    public void onCombust(final EntityDamageEvent e) {
        if (!(e.getEntity() instanceof Item)) {
            return;
        }
        final Item item = (Item) e.getEntity();
        if (item.isDead()) {
            return;
        }
        item.remove();
        sellItem(item.getItemStack(), "damaged cause: " + e.getCause().toString(), item);
    }

    static {
        ItemListener.pages = new HashMap<>();
    }
}
